package com.ht.controller;

import com.alibaba.fastjson.JSON;
import com.ht.bean.*;
import com.ht.service.NoticeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    /*
     *教师端公告信息详情
     * @param
     * @return
     **/
    @RequestMapping("/noticeList")
    public String noticeList(Model model) {
        List<Notice> list = noticeService.selectNoticeMsg();
        model.addAttribute("noticelist", list);
        System.out.println(list);
        return "home";
    }

    /*
     *学生端公告信息
     * @param
     * @return
     **/
    @RequestMapping("/noticeLists")
    public String noticeLists(Model model) {
        List<Notice> list = noticeService.selectNoticeMsgs();
        model.addAttribute("noticelist", list);
        System.out.println(list);
        return "stuhome";
    }

    /*
     *去查看公告信息
     * @param
     * @return
     **/
    @RequestMapping("/goLookNoticeContent/{noticeId}")
    private String goLookWeeklyContent(Model model, @PathVariable Integer noticeId) {
        List<Map> list = noticeService.selectNoticeByNoticeId(noticeId);
        model.addAttribute("list", list);
        System.out.println(list);
        return "LookNoticeContent";
    }

    //查看公告方法
    @RequestMapping("/golookover")
    @ResponseBody
    public Map chaNotice(Integer noticeId, Noticereceiver noticereceiver, HttpSession session, HttpServletRequest request) {
        Map map = new HashMap();
        Notice notice = noticeService.selectNoticeById(noticeId);
        Emp emp = (Emp) session.getAttribute("emp");
        System.out.println(emp + "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
        if (emp==null){
        if (notice.getNoticeType() == 2) {
            Student student = (Student) session.getAttribute("STU");
            noticereceiver.setReceiver(student.getStuId().toString());
            boolean bln = noticeService.updnoticereceivers(noticereceiver);
            ;
        } else if (notice.getNoticeType() == 3) {
            Student student = (Student) session.getAttribute("STU");
            noticereceiver.setReceiver(student.getStuId().toString());
            boolean bln = noticeService.updnoticereceiveres(noticereceiver);
            ;
        }
        }else {
            noticereceiver.setReceiver(emp.getEmpId().toString());
            System.out.println(noticereceiver.getNoticeId()+"=="+notice.getNoticeId()+" "+ noticereceiver.getReceiver()+"=="+emp.getEmpId().toString());
            boolean bln = noticeService.updnoticereceiver(noticereceiver);
        }
        return map;
    }


    /*
     *去员工信息页面
     * @param
     * @return
     **/
    @RequestMapping("/gonoticeMsgList")
    public String noticeMsgList(Model model) {


        return "noticeMsgList";
    }

    /*
     *去添加公告
     * @param
     * @return
     **/
    @RequestMapping("/goaddNotice")
    public String goaddNotice(Model model) {
        List<Studentclass> studentclassList = noticeService.studentclassList();
        model.addAttribute("studentclassList", studentclassList);
        return "addNotice";
    }

    /*
     *查询所有公告信息
     * @param
     * @return
     **/
    @RequestMapping("/noticeMsgList")
    @ResponseBody
    public Map noticeMsgList(Integer page, Integer limit, Notice notice, Emp emp) {
        List<Map> list = noticeService.selectNoandEmp((page - 1) * limit, limit, emp, notice);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "成功");
        map.put("count", noticeService.countNoandEmp(emp, notice));
        map.put("data", list);
        return map;
    }

    /*
     *删除员工信息
     * @param
     * @return
     **/
    @RequestMapping("/deleteNoticeIds")
    @ResponseBody
    public Map deleteNoticeId(Integer noticeId) {
        Map map = new HashMap();
        boolean bln = noticeService.deleteNoticeId(noticeId);
        boolean blns = noticeService.deleteNoticereceiverId(noticeId);
        if (bln) {
            map.put("msg", "删除成功！");
        } else {
            map.put("msg", "删除失败！");
        }
        return map;
    }

    /*
     *员工信息页面
     * @param
     * @return
     **/
    @RequestMapping("/addNoticeNews")
    @ResponseBody
    public Map addNoticeNews(Notice notice, Noticereceiver noticereceiver) {
        Map map = new HashMap();
        boolean bln = noticeService.insertNoticeMsg(notice);
        if (bln) {
            map.put("msg", "新增成功。");
        } else {
            map.put("msg", "新增失败。");
        }
        Integer noticeID = notice.getNoticeId();
        System.out.println("公告ID：" + noticeID + "");
        Integer[] emp1 = noticeService.selectAllEmpId();
        Integer[] student = noticeService.selectAllStuId();
        Integer[] student2 = noticeService.selectAllClaStuId(notice);
        System.out.println("有学生：" + student.length + "个");
        System.out.println("有员工：" + emp1.length + "个");
        if (notice.getNoticeType() == 1) {
            for (int i = 0; i <= emp1.length - 1; i++) {
                noticereceiver.setReceiver((emp1[i]).toString());
                noticereceiver.setNoticeId(notice.getNoticeId());
                noticereceiver.setIsRead(2);
                noticereceiver.setIsStudent(1);
                boolean bln1 = noticeService.insertNoticeReceivce(noticereceiver);
            }
        } else if (notice.getNoticeType() == 2) {
            for (int i = 0; i <= student.length - 1; i++) {
                noticereceiver.setReceiver((student[i]).toString());
                noticereceiver.setNoticeId(notice.getNoticeId());
                noticereceiver.setIsRead(2);
                noticereceiver.setIsStudent(2);
                boolean bln1 = noticeService.insertNoticeReceivces(noticereceiver);
            }
        } else if (notice.getNoticeType() == 3) {
            for (int i = 0; i <= student2.length - 1; i++) {
                noticereceiver.setReceiver((student[i]).toString());
                noticereceiver.setNoticeId(notice.getNoticeId());
                noticereceiver.setIsRead(2);
                noticereceiver.setIsStudent(3);
                boolean bln1 = noticeService.insertNoticeReceivces(noticereceiver);
            }
        }
        return map;
    }


    /*
     *去修改员工信息
     * @param
     * @return
     **/
    @RequestMapping("/goupdNotice/{noticeId}")
    public String goupdateempmessage(@PathVariable("noticeId") Integer noticeId, Model model) {
        Notice notice = noticeService.selectNoticeById(noticeId);//将emp表中的empid带到修改信息界面中
        model.addAttribute("notice", JSON.toJSON(notice));//将对象转换成JSON对象，传到前端
        return "updNotice";
    }

    /*
     *修改公告信息
     * @param
     * @return
     **/
    @RequestMapping("/updNotice")
    @ResponseBody
    public Map updateempmessage(Notice notice) {
        Map map = new HashMap();
        notice.setClazz("1");
        notice.setNoticeType(1);
        notice.setEmpIds("1");
        notice.setNoticeTime(new Date().toLocaleString());
        boolean bln = noticeService.updateNoticeId(notice);
        if (bln) {
            map.put("msg", "修改成功。");
        } else {
            map.put("msg", "修改失败。");
        }
        return map;
    }

    /*
     *
     * @param
     * @return
     **/
    @RequestMapping("/goover/{noticeId}")
    public String goover(@PathVariable("noticeId") Integer noticeId, Notice notice, Model model) {
        notice = noticeService.selectNoticeById(noticeId);
        if (notice.getNoticeType() == 1) {
            Integer isStudent = notice.getNoticeType();
            List<Map> sylist = noticeService.allsynotice(notice, isStudent);
            System.out.println("审阅详情：" + sylist);
            Map map = new HashMap();
            map.put("code", 0);
            map.put("msg", "成功");
            map.put("count", noticeService.countNotice());//总页数
            map.put("data", sylist);
            model.addAttribute("overlist", sylist);
            return "goover";
        } else if (notice.getNoticeType() == 2) {
            Integer isStudent = notice.getNoticeType();
            System.out.println("全体学生发送" + notice.getNoticeType());
            List<Map> sylist = noticeService.allsynotices(notice, isStudent);
            System.out.println("审阅详情：" + sylist);
            Map map = new HashMap();
            map.put("code", 0);
            map.put("msg", "成功");
            map.put("count", noticeService.countNotice());//总页数
            map.put("data", sylist);
            model.addAttribute("overlist", sylist);

        } else if (notice.getNoticeType() == 3) {
            Integer isStudent = notice.getNoticeType();
            System.out.println("班级发送" + notice.getNoticeType());
            List<Map> sylist = noticeService.allsynoticeies(notice, isStudent);
            System.out.println("审阅详情：" + sylist);
            Map map = new HashMap();
            map.put("code", 0);
            map.put("msg", "成功");
            map.put("count", noticeService.countNotice());//总页数
            map.put("data", sylist);
            model.addAttribute("overlist", sylist);
        }
        return "goover";
    }

}
